﻿	<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
	<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
	
	<script type="text/javascript">
		var storage_goodsName_formatter = function(value,row,index) 
		{
			if(row.goodsBean) 
			{
				return row.goodsBean.name;
			}
			return row.goodsName;
		}
		var storage_goodsId_formatter = function(value,row,index) 
		{
			if(row.goodsBean) 
			{
				return row.goodsBean.id;
			}
		}
		function cellStyler_goodsName(value,row,index)
		{
			if(row.goods) 
			{
				return "color:blue";
			}
		}
		var cellFormatter_goodsStock = function(value,row,index) 
		{
			if(row.goods) 
			{
				return value;
			}
		}
		var orderStatusFormatter2 = function (value, row, index) 
		{
			if(value == '0') 
			{
				return '<font color="blue">新建</font>';
			}
			else if(value == '1') 
			{
				return '<font color="red">退回</font>';
			}
			else if(value == '2') 
			{
				return '<font color="orange">取消</font>';
			}
			else if(value == '3') 
			{
				return '<font color="green">完成</font>';
			}
		}
		var orderTooltipFormatter = function (value, row, index) 
		{
			return "<a href='javascript:void(0)' id='"+value+"' title='点击查看明细'>" + value + "</a>";
		}
		var showOrderDetail = function (orderId) 
		{
			//console.log(orderId);
			$('#dlg-view-storage-course a#' + orderId).tooltip({
                content: $('<div></div>'),
                showEvent: 'click',
                onUpdate: function(content){
                    content.panel({
                        width: 300,
                        border: false,
                        noheader: true,
                        title: '订单明细',
						method: 'post',
						queryParams:{orderId:orderId, templateFile:'orderTableNoMoneyTemplate.html'},
                        href: '<c:url value='/outOrder/getOrderItemTable.html' />'
                    });
                },
                onShow: function(){
                    var t = $(this);
                    t.tooltip('tip').unbind().bind('mouseenter', function(){
                        t.tooltip('show');
                    }).bind('mouseleave', function(){
                        t.tooltip('hide');
                    });
                }
            });
		}
		
		var viewStorageCallback = function () 
		{
			var row = $('#dg-storage').datagrid('getSelected');
			if(row) 
			{
				$('#dlg-storage #fm-storage #goodsName').val (row.goodsBean.name);
			}
		}
		
		var onGoodsStorageRowContextMenu = function(e, row) 
		{
			e.preventDefault();
			$(this).treegrid('select',row.id);
			if(row.goods) 
			{
				$('#mm_goodsStorage').menu('show',{
					left: e.pageX,
					top: e.pageY
				});
			}
		}
		
		var onBeforeLoadStorageCourse = function (param) 
		{
			//var startDate = $('#toolbar-customer-payment #advanceSearchSpan #startDate').combo('getValue');
			//var endDate = $('#toolbar-customer-payment #advanceSearchSpan #endDate').combo('getValue');
			//var customerId = $('#toolbar-customer-payment #advanceSearchSpan #customerId').combo('getValue');
			/*if(startDate == '' && endDate == '' && customerId == '') 
			{
				var timeFrame = $('#toolbar-customer-payment #advanceSearchSpan #timeFrame').combo('getValue');
				startDate = generateStartDate(timeFrame);
				endDate = new Date().format("yyyy-MM-dd");
				$('#toolbar-customer-payment #advanceSearchSpan #startDate').datebox('setValue', startDate);
				$('#toolbar-customer-payment #advanceSearchSpan #endDate').datebox('setValue', endDate);
				
				startDate = $('#toolbar-customer-payment #advanceSearchSpan #startDate').combo('getValue');
				endDate = $('#toolbar-customer-payment #advanceSearchSpan #endDate').combo('getValue');
			}*/
			
			var goodsStorage = $('#dg-storage').datagrid('getSelected');
			$('#dg-storage-course').datagrid('options').url = "<c:url value='/goodsStorage/getStorageCourse.html' />?goodsId="+goodsStorage.goodsBean.id;
			return true;
		}
		var viewStorageCourse = function() 
		{
			var goodsStorage = $('#dg-storage').datagrid('getSelected');
			if (goodsStorage && goodsStorage.goods) {
				$('#dlg-view-storage-course').dialog('open').dialog('setTitle', '货物' + goodsStorage.goodsBean.name + '的经营历程');
				
				$('#dg-storage-course').datagrid('options').onBeforeLoad = onBeforeLoadStorageCourse;
				$('#dg-storage-course').datagrid('reload');
			}
		}
		var onLoadStorageCourseSuccess = function(data) 
		{
			for(var i = 0; i < data.rows.length; i ++) 
			{
				showOrderDetail(data.rows[i].orderId);
			}
		}
		var viewStorageRest = function () 
		{
			$('#dlg-view-storage-rest').dialog('open');
			
			checkRest($('#tb-view-storage-rest #checkRest')[0]);
		}
		var printStorageRest = function() 
		{
			$('#dlg-view-storage-rest #inorder-table').print();
		}
		var checkRest = function (cbox) 
		{
			$('#dlg-view-storage-rest').mask('加载中...');
			var requestUrl = '<c:url value='/goodsStorage/getStorageRest.html' />';
			var param = cbox.checked ? {showRest : '1'} : {showRest : '0'};
			$.post(requestUrl, param, 
				function(result) 
				{
					$('#dlg-view-storage-rest').unmask();
					//console.log(result);
					var tableHtml = "<table border='1px' style='width:100%; border-collapse:collapse;'>";
					var tableCaption = "<caption style='font-size:20px; font-weight:bold; text-align:center;'>库存清点</caption>";
					tableHtml += tableCaption;
					
					var tableHead = "<thead><tr align='left'>";
					tableHead += "<th width='6%'>" + "序号" + "</th>";
					tableHead += "<th width='25%'>" + "货物名" + "</th>";
					tableHead += "<th width='15%'>" + "货物编码" + "</th>";
					tableHead += "<th width='12%'>" + "货物类型" + "</th>";
					tableHead += "<th width='12%'>" + "所在仓库" + "</th>";
					tableHead += "<th width='11%'>" + "当前库存" + "</th>";
					tableHead += "<th width='19%'>" + "清点结果" + "</th>";
					tableHead += "</tr></thead>";
					tableHtml += tableHead;
					
					var tableBody = "<tbody>";
					//var interval = 2;
					//var exactDivision = Math.floor(result.rows.length / interval);
					//var remainCount = result.rows.length % interval;
					var tr = "";
					for(var i = 0; i < result.rows.length; i++) 
					{
						tr = "";
						tr += "<tr>";
						tr += "<td>" + (i+1) + "</td>";
						tr += "<td style='white-space:normal; word-break:break-all;overflow:hidden;'>" + result.rows[i].name + "</td>";
						tr += "<td>" + result.rows[i].barCode + "</td>";
						if(result.rows[i].typeBean) {
							tr += "<td>" + result.rows[i].typeBean.text + "</td>";
						} else {
							tr += "<td>" + "" + "</td>";
						}
						if(result.rows[i].depositoryBean) 
						{
							tr += "<td>" + result.rows[i].depositoryBean.name + "</td>";
						}
						else 
						{
							tr += "<td>" + "" + "</td>";
						}
						tr += "<td>" + result.rows[i].storageBean.currentStock + "</td>";
						tr += "<td>" + "平&nbsp;&nbsp;&nbsp;盈&nbsp;&nbsp;&nbsp;亏" + "</td>";
						
						tr += "</tr>";
						tableBody += tr;
					}
					
					tableBody += "</tbody>";
					tableHtml += tableBody;
					
					var tableFooter = "<tfoot style='font-weight:bold; font-style:italic;'>";
					var goodsCount = result.total;
					//console.log(goodsCount);
					var tr = "";
					tr += "<tr>";
					tr += "<td>" + "" + "</td>";
					tr += "<td>" + "货物总数" + "</td>";
					tr += "<td>" + result.rows.length + "</td>";
					tr += "<td>" + "日期" + "</td>";
					tr += "<td>" + "" + "</td>";
					tr += "<td>" + "清点人" + "</td>";
					tr += "<td>" + "" + "</td>";
					tr += "</tr>";
					tableFooter += tr;
					tableFooter += "</tfoot>";
					tableHtml += tableFooter;
					
					tableHtml += "</table>";
					//console.log (tableHtml);
					var $table = $('#dlg-view-storage-rest #inorder-table table');
					if($table.get(0)) 
					{
						$table.replaceWith($(tableHtml));
					}
					else 
					{
						$('#dlg-view-storage-rest #inorder-table').append($(tableHtml));
					}
				}, 
			'json');
		}
	</script>
	
	<table id="dg-storage" title="<spring:message code="goodsStorage.management" />" class="easyui-treegrid" url="<c:url value='/goodsStorage/asyncGetStorages.html' />" 
		rownumbers="true" singleSelect="true" checkOnSelect="true" selectOnCheck="false" showFooter="true" toolbar="#toolbar-storage" 
		data-options="fitColumns:true, idField:'id',treeField:'goodsName', fit:true, onContextMenu:onGoodsStorageRowContextMenu">
		<thead>
			<tr>
				<!-- <th data-options="field:'ck',checkbox:true"></th> -->
				<th data-options="field:'id',hidden:true"></th>
				<th field="goodsId" data-options="hidden:true, formatter:storage_goodsId_formatter"></th>
				<th field="goodsName" width="200" data-options="styler:cellStyler_goodsName, formatter:storage_goodsName_formatter"><spring:message code="goodsStorage.goods" /></th>
				<th field="initialStock" width="50" data-options="formatter:cellFormatter_goodsStock"><spring:message code="goodsStorage.initialStock" /></th>
				<th field="currentStock" width="50" data-options="formatter:cellFormatter_goodsStock"><spring:message code="goodsStorage.currentStock" /></th>
				<th field="minStock" width="50" data-options="formatter:cellFormatter_goodsStock"><spring:message code="goodsStorage.minStock" /></th>
				<th field="maxStock" width="50" data-options="formatter:cellFormatter_goodsStock"><spring:message code="goodsStorage.maxStock" /></th>
				<th field="createTime" width="50"><spring:message code="goodsStorage.createDate" /></th>
				<th field="updateTime" width="50"><spring:message code="goodsStorage.updateDate" /></th>
				<th field="userCreated" width="50">创建人</th>
				<th field="userUpdated" width="50"><spring:message code="goodsStorage.updateUser" /></th>
				<th field="comment" width="70"><spring:message code="goodsStorage.comment" /></th>
			</tr>
		</thead>
	</table>
	<div id="toolbar-storage">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" plain="true" 
			onclick="viewStorageCourse()" title="查看经营历程">查看经营历程</a> 
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" plain="true" 
			onclick="viewStorageRest()" title="盘库存">盘库存</a> 
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" 
			onclick="javascript:$('#toolbar-storage #storageSearchBox').searchbox('clear');doSearchStorage('');" title="<spring:message code="refresh" />"><spring:message code="refresh" /></a>
		<span style="float: right; margin-right: 5px">
			<input id="storageSearchBox" class="easyui-searchbox" data-options="prompt:'<spring:message code="pleaseInputValue" />',searcher:doSearchStorage" />
		</span> 
	</div>
	
	<!-- 右键菜单 -->
	<div id="mm_goodsStorage" class="easyui-menu" style="width:120px;">
        <div data-options="iconCls:'icon-search'" onclick="viewStorageCourse()">查看经营历程</div>
    </div>
	<!-- 右键菜单 -->

	<!-- 查看库存历程 -->
	<div id="dlg-view-storage-course" class="easyui-dialog"
		style="width: 800px; height: 500px; padding: 5px;" closed="true"
		buttons="#dlg-buttons-view-storage-course" data-options="modal:true">
		<table id="dg-storage-course" class="easyui-datagrid"
			style="" url="" rownumbers="true"
			singleSelect="true" fitColumns="true" data-options="fit:true, pagination:true, onLoadSuccess:onLoadStorageCourseSuccess">
			<thead>
				<tr>
					<th data-options="field:'goodsId',hidden:true"></th>
					<th field="goodsName" width="50" data-options="styler:cellStyler_goodsNameBlue">货物</th>
					<th field="orderId" width="50" data-options="formatter:orderTooltipFormatter">订单号</th>
					<th field="orderCreateDate" width="60">下单时间</th>
					<th field="customerId" hidden="true"></th>
					<th field="customerName" width="50">客户或供应商</th>
					<th field="orderTypeCode" width="30" data-options="formatter:orderTypeFormatter">订单类型</th>
					<th field="orderStatusCode" width="30" data-options="formatter:orderStatusFormatter2">订单状态</th>
					<th field="amount" width="30">数量</th>
					<th field="goodsPrice" width="30">单价</th>
					<!-- <th field="currentStock" width="50">当前库存</th> -->
				</tr>
			</thead>
		</table>
	</div>
	<div id="dlg-buttons-view-storage-course">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#dlg-view-storage-course').dialog('close')"><spring:message code="close" /></a>
	</div>
	<!-- 查看库存历程 -->
	
	<!-- 盘库存 -->
	<div id="dlg-view-storage-rest" class="easyui-dialog" title="盘库存" 
		style="width: 800px; height: 500px; padding: 5px;" closed="true"
		buttons="#dlg-buttons-view-storage-rest" toolbar="#tb-view-storage-rest" data-options="modal:true">
		<div id="inorder-table" style="margin:15px;"></div>
	</div>
	<div id="tb-view-storage-rest">
		<label><input id="checkRest" type="checkbox" checked="checked" style="width:20px" onclick="checkRest(this)">显示剩余</label>
	</div>
	<div id="dlg-buttons-view-storage-rest">
		<a href="javascript:void(0)" id="" class="easyui-linkbutton"
			iconCls="icon-back" onclick="">导出</a> 
		<a href="javascript:void(0)" id="" class="easyui-linkbutton"
			iconCls="icon-print" onclick="printStorageRest()"><spring:message code="print" /></a> 
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" 
			onclick="javascript:$('#dlg-view-storage-rest').dialog('close')"><spring:message code="close" /></a>
	</div>
	<!-- 盘库存 -->
